package com.markus.code.二叉树.leetcode;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/10 21:32
 * todo: 判断二叉树是否是对称二叉树
 */
public class LeetCode_101 {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }
//    public boolean isSymmetric(TreeNode root) {
//        if (root == null){
//            return true;
//        }
//        TreeNode node = new TreeNode(-1);
//        node = root;
//        process_1(node);
//        return process_2(root,node);
//    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null){
            return true;
        }
        return process_2(root.left,root.right);
    }
    private void process_1(TreeNode node){
        if (node != null){
            return ;
        }
        swap(node);
        process_1(node.left);
        process_1(node.right);
    }
    private boolean process_2(TreeNode a,TreeNode b){
        if (a == null){
            return true;
        }
        if (b == null){
            return false;
        }
        if (a.val != b.val){
            return false;
        }
        return process_2(a.left,b.right) && process_2(a.right,b.left);
    }
    private void swap(TreeNode x){
        TreeNode temp = x.left;
        x.left = x.right;
        x.right = temp;
    }

    public static void main(String[] args) {
        TreeNode A = new TreeNode(1);
        A.left = new TreeNode(2);
        A.right = new TreeNode(2);
        A.left.left = new TreeNode(3);
        A.left.right = new TreeNode(4);
        A.right.left = new TreeNode(4);
        A.right.right = new TreeNode(3);
        LeetCode_101 demo = new LeetCode_101();
        System.out.println(demo.isSymmetric(A));
    }
}
